/**
 * Generali Solutions d'assurances - Tous droits réservés &copy; 2007 - 2010
 */
package fr.generali.accueilclient.commun.editique;

import java.util.Collection;
import java.util.List;
import java.util.ListIterator;
import java.util.RandomAccess;

/**
 * @author Holmes Kelly
 */
public final class CollectionsHelper {

    /**
     * 
     */
    private static final int COPY_THRESHOLD = 10;

    /**
     * Constructeur privee
     */
    private CollectionsHelper() {

    }

    /**
     * Copies all of the elements from one list into another. After the
     * operation, the index of each copied element in the destination list will
     * be identical to its index in the source list. The destination list must
     * be at least as long as the source list. If it is longer, the remaining
     * elements in the destination list are unaffected.
     * <p>
     * This method runs in linear time.
     * @param <T> .
     * @param dest The destination list.
     * @param src The source list.
     * 
     * @return boolean isCopyValide
     */
    public static <T> Boolean copy(List< ? super T> dest, List< ? extends T> src) {

        Boolean isCopyValide = null;

        int srcSize = src.size();
        if (srcSize > dest.size()) {
            isCopyValide = false;
            throw new IndexOutOfBoundsException("Source does not fit in dest");

        }

        if (srcSize < COPY_THRESHOLD || (src instanceof RandomAccess && dest instanceof RandomAccess)) {
            for (int i = 0; i < srcSize; i++) {
                dest.set(i, src.get(i));
            }
        } else {
            ListIterator< ? super T> di = dest.listIterator();
            ListIterator< ? extends T> si = src.listIterator();
            for (int i = 0; i < srcSize; i++) {
                di.next();
                di.set(si.next());
            }
        }

        isCopyValide = true;

        return isCopyValide;
    }

    /**
     * Adds all elements in the iteration to the given collection.
     * 
     * @param <T> classe parametre
     * @param collectionDest the collection to add to, must not be null
     * @param collectionSrc collection src
     * @return boolean check
     */
    public static <T> Boolean copyAll(Collection< ? super T> collectionDest,
                    Collection< ? extends T> collectionSrc) {

        return collectionSrc == null ? false : collectionDest.addAll(collectionSrc);
    }
}
